package com.example.tourpal.volley;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.example.tourpal.R;

public class VolleyTestHelp {
	public void TestGetStringInterface(Context context) {
		StringRequest stringRequest = new StringRequest("http://www.baidu.com",
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						Log.d("TAG", response);
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				});
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				stringRequest);
	}

	public void TestPostStringInterface(Context context) {
		StringRequest stringRequest = new StringRequest(Method.POST,
				"http://www.baidu.com", new Response.Listener<String>() {

					@Override
					public void onResponse(String response) {
						Log.d("TAG", response);
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = new HashMap<String, String>();
				map.put("params1", "value1");
				map.put("params2", "value2");
				return map;
			}
		};
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				stringRequest);
	}

	public void TestGetJsonInterface(Context context) {
		// JsonArrayRequest
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				"http://m.weather.com.cn/data/101010100.html", null,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						Log.d("TAG", response.toString());
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				});
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				jsonObjectRequest);
		
		// POST
		Map<String, String> map = new HashMap<>();
		map.put("type", "yuantong");
		map.put("postid", "229728279823");
		// 将map转化为JSONObject对象
		JSONObject jsonObject = new JSONObject(map);
		JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(
				Method.POST, "", jsonObject,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						Log.d("TAG", response.toString());
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				});
	}

	public void TestGetImageInterface(Context context, final ImageView imageView) {
		ImageRequest imageRequest = new ImageRequest(
				"http://developer.android.com/images/home/aw_dac.png",
				new Response.Listener<Bitmap>() {
					@Override
					public void onResponse(Bitmap response) {
						imageView.setImageBitmap(response);
					}
				}, 0, 0, Config.RGB_565, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						imageView.setImageResource(R.drawable.ic_launcher);
					}
				});
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				imageRequest);
	}

	// networkImageView.setDefaultImageResId(R.drawable.default_image);
	// networkImageView.setErrorImageResId(R.drawable.failed_image);
	// networkImageView.setImageUrl("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",
	// imageLoader);

	public void TestGetImageLoaderInterface(Context context,
			final ImageView imageView) {
		ImageLoader imageLoader = VolleySingleton.getVolleySingleton(context)
				.getImageLoader();
		ImageListener listener = ImageLoader.getImageListener(imageView,
				R.drawable.ic_launcher, R.drawable.ic_launcher);
		// imageLoader.get("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",
		// listener);
		imageLoader
				.get("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",
						listener, 200, 200);
	}

	public void TestXMLRequestInterface(Context context) {
		XMLRequest xmlRequest = new XMLRequest(
				"http://flash.weather.com.cn/wmaps/xml/china.xml",
				new Response.Listener<XmlPullParser>() {
					@Override
					public void onResponse(XmlPullParser response) {
						try {
							int eventType = response.getEventType();
							while (eventType != XmlPullParser.END_DOCUMENT) {
								switch (eventType) {
								case XmlPullParser.START_TAG:
									String nodeName = response.getName();
									if ("city".equals(nodeName)) {
										String pName = response
												.getAttributeValue(0);
										Log.d("TAG", "pName is " + pName);
									}
									break;
								}
								eventType = response.next();
							}
						} catch (XmlPullParserException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				});
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				xmlRequest);
	}

	public void TestGsonRequestInterface(Context context) {
		GsonRequest<Weather> gsonRequest = new GsonRequest<Weather>(
				"http://www.weather.com.cn/data/sk/101010100.html",
				Weather.class, new Response.Listener<Weather>() {
					@Override
					public void onResponse(Weather weather) {
						WeatherInfo weatherInfo = weather.getWeatherinfo();
						Log.d("TAG", "city is " + weatherInfo.getCity());
						Log.d("TAG", "temp is " + weatherInfo.getTemp());
						Log.d("TAG", "time is " + weatherInfo.getTime());
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("TAG", error.getMessage(), error);
					}
				});
		VolleySingleton.getVolleySingleton(context).addToRequestQueue(
				gsonRequest);
		
	}

	public class Weather {

		private WeatherInfo weatherinfo;

		public WeatherInfo getWeatherinfo() {
			return weatherinfo;
		}

		public void setWeatherinfo(WeatherInfo weatherinfo) {
			this.weatherinfo = weatherinfo;
		}

	}

	public class WeatherInfo {

		private String city;

		private String temp;

		private String time;

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public String getTemp() {
			return temp;
		}

		public void setTemp(String temp) {
			this.temp = temp;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

	}

}
